package com.tanhua.server.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.server.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author Au
 * @DATE 2020/11/16 21:05
 **/
@Service
public class UserService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${tanhua.sso.url}")
    private String url;

    private static final ObjectMapper mapper = new ObjectMapper();

    /**
     * 调用sso系统的接口服务进行查询
     * @param token
     * @return
     */
    public User queryUserByToken(String token) {
        String json = restTemplate.getForObject(url + "/user/" + token, String.class);
        if (StringUtils.isNotBlank(json)) {
            try {
                User user = mapper.readValue(json, User.class);
                return user;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;

    }
}
